package com.redis.redisinfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.redis.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.CountDownLatch;

/**
 * @Author： yu.Tan
 * @Date: 2020/9/1 10:24
 * @Description:
 */
public class ReceiverRedisMessage {

    private static final Logger log = LoggerFactory.getLogger(ReceiverRedisMessage.class);

    private CountDownLatch latch;

    @Autowired
    public ReceiverRedisMessage(CountDownLatch latch) {
        this.latch = latch;
    }

    public void receiveMessage(String jsonMsg) {
        log.info("[开始消费REDIS消息队列phone数据...]");
        try {
            log.info("监听者收到消息：{}", jsonMsg);
            JSONObject exJson = JSONObject.parseObject(jsonMsg);
            User user = JSON.toJavaObject(exJson, User.class);
            System.out.println("转化为对象 ："+user);
            log.info("[消费REDIS消息队列cctv1数据成功.]");
        } catch (Exception e) {
            log.error("[消费REDIS消息队列cctv1数据失败，失败信息:{}]", e.getMessage());
        }
        latch.countDown();
    }
}
